package com.zktr.service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.zktr.bean.CategoryBean;
import com.zktr.dao.CategoryDao;
import com.zktr.pojo.Category;
import com.zktr.pojo.Goods;
import com.zktr.pojo.PageBean;

public class CategoryService {
	
	private CategoryDao dao=new CategoryDao();
	

	public static void main(String[] args) throws Exception {
		CategoryService cService = new CategoryService();
		System.out.println(cService.selectCateGoods("手机"));
	/*	PageBean<CategoryBean> findAllGoods = cService.findAllCategory(1, 10);
		
		System.out.println(findAllGoods.getBeanList());*/
	}
	/**
	 * 查询父分类
	 * @return
	 */
	public List<Category> queryParents() {
		return dao.selectParent().get(0);
	}
	/**
	 * 查询子分类
	 * @return 
	 */
	public List<Category> queryChilds(int id) {
		try {
			return dao.selectChilds(id).get(0);
		} catch (Exception e) {
			throw new RuntimeException("没有子类");
		}
	}
	/**
	 * 查询所有的分类，并封装到map集合中
	 * 用map集合绑定三级分类关系
	 * 此方法还存在问题
	 * @return 
	 */
	public Map<Category, Map<Category, List<Category>>> queryAll() {
		Map<Category,Map<Category,List<Category>>> result=new LinkedHashMap<>();
		Map<Category,List<Category>> map=new LinkedHashMap<>();
		List<Category> queryParents = this.queryParents();
		for (Category category : queryParents) {
			List<Category> queryChilds = this.queryChilds(category.getCatid());
			for (Category category2 : queryChilds) {
				List<Category> queryChilds2 = this.queryChilds(category2.getCatid());
				map.put(category2, queryChilds2);
				result.put(category, map);
			}
		}
		return result;
	}
	/**
	 * 根据父类id查询想对应的二三级分类
	 * @return 
	 */
	public Map<Category, List<Category>> queryTwoCategory(int id) {
		Map<Category,List<Category>> map=new LinkedHashMap<>();
		List<Category> queryChilds = this.queryChilds(id);
		for (Category category : queryChilds) {
			List<Category> queryChilds2 = this.queryChilds(category.getCatid());
			map.put(category, queryChilds2);
		}
		return map;
	}
	/**
	 * 模糊查询
	 * @return 
	 */
	public List<Category> queryLike(String name) {
		return dao.selectLike(name);
	}
	public PageBean<CategoryBean> findAllCategory(int pc, int ps) {
		List<Category> selectGoods = dao.queryAllCategory(pc, ps);
		List<CategoryBean> list2 = new LinkedList<>();
		findAllCategory(selectGoods,list2);
		PageBean<CategoryBean> bean = new PageBean<CategoryBean>();
		bean.setPc(pc);
		bean.setPs(ps);
		bean.setBeanList(list2);
		bean.setTr(dao.selectCategoryCount());
		return bean;
	}
	  private static void findAllCategory(List<Category> list, List<CategoryBean> list2) {
			for (Category goods : list) {
				CategoryBean cBeans = new CategoryBean();
				cBeans.setCatid(goods.getCatid()+"");
				cBeans.setParentid(goods.getParentid()+"");
				cBeans.setCatname(goods.getCatname());
				cBeans.setGrade(goods.getGrade()+"");
				cBeans.setCatdesc(goods.getCatdesc());
				cBeans.setKeywords(goods.getKeywords());
				if (goods.getIsshow()) {
					cBeans.setIsshow("../img/true.png");
				}else {
					cBeans.setIsshow("../img/false.png");
				}
				if (goods.getShowinnav()) {
					cBeans.setShowinnav("../img/true.png");
				}else {
					cBeans.setShowinnav("../img/false.png");
				}
				list2.add(cBeans);
			}
			
	    }
		/**
		 * 查询类别名称
		 * @return
		 */
		public List<Category> queryAllCategoryName() {
			return dao.queryAllCategoryName();
		}
	  /**
	 * @param catname
	 * @return 文件地址
	 * @throws Exception
	 */
	public String fileaddress(String catname) throws Exception{
		  return dao.fileaddress(catname);
	  }
	public boolean selectCateGoods(String name) {
		boolean flag=true;
		List<Goods> selectCateGoods = dao.selectCateGoods(name);
		if (selectCateGoods.size()==0) {
			return !flag;
		}else {
			return flag;
		}
	}

}
